﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using ZYCommon;

namespace TextEditor
{
    public class ZYTextTableRow : ZYTextContainer
    {
        private ZYTextTable ownerTable;

        public ZYTextTableRow()
            : base(0)
        {
            this.ownerTable = null;
            base.ID = StringCommon.AllocObjectName();            
        }

        public ZYTextTableRow(ZYTextTable ownerTable, ZYTextTableRow refRow, int pos)
        {
            this.ownerTable = null;
            if (ownerTable != null)
            {
                this.ownerTable = ownerTable;
                this.Parent = ownerTable;
                this.OwnerDocument = ownerTable.OwnerDocument;
                for (int i = 0; i < refRow.CellCount; i++)
                {
                    ZYTextTableCell cell = ownerTable.CreateCell(this);
                    cell.Width = ((ZYTextTableCell) refRow.Cells[i]).Width;
                    cell.Height = ((ZYTextTableCell) refRow.Cells[i]).Height;
                    this.AppendCell(cell);
                }
                int index = ownerTable.Rows.IndexOf(refRow);
                if (pos == 0)
                {
                    index--;
                }
                ownerTable._InsertRow(index, this);
            } 
            base.ID = StringCommon.AllocObjectName();            
        }

        public bool AppendCell(ZYTextTableCell cell)
        {
            this.Cells.Add(cell);
            return true;
        }

        public void AverageCellWidth()
        {
            int num = base.intWidth - 11;
            int num2 = num / this.CellCount;
            int num3 = num % this.CellCount;
            float num4 = num3 / this.CellCount;
            if ((num4 > 0f) && (num4 < 1f))
            {
                num4 = 1f;
            }
            foreach (ZYTextTableCell cell in this.Cells)
            {
                cell.Width = num2;
                if (num3 > 0)
                {
                    cell.Width += (int) num4;
                    num3--;
                }
            }
        }

        public void DecHeight(int decHeight)
        {
            this.ModifyHeight(-decHeight);
        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                foreach (ZYTextElement element in this.Cells)
                {
                    if (element is ZYTextTableCell)
                    {
                        ZYTextTableCell cell = element as ZYTextTableCell;
                        cell.OwnerTable = this.OwnerTable;
                        cell.OwnerRow = this;
                    }
                }
            }
            return true;
        }

        public ZYTextTableCell GetNextCell(ZYTextTableCell refCell)
        {
            if (this.IsLastCell(refCell))
            {
                return null;
            }
            return (ZYTextTableCell) this.Cells[refCell.ColIndex + 1];
        }

        public ZYTextTableCell GetPreCell(ZYTextTableCell refCell)
        {
            if (this.IsFirstCell(refCell))
            {
                return null;
            }
            return (ZYTextTableCell) this.Cells[refCell.ColIndex - 1];
        }

        public override string GetXMLName()
        {
            return "row";
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            return base.HandleClick(x, y, Button);
        }

        public override bool HandleDblClick(int x, int y, MouseButtons Button)
        {
            return base.HandleDblClick(x, y, Button);
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseDown(x, y, Button);
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseMove(x, y, Button);
        }

        public override bool HandleMouseUp(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseUp(x, y, Button);
        }

        public void IncHeight(int incHeight)
        {
            this.ModifyHeight(incHeight);
        }

        public bool InsertCell(int index, ZYTextTableCell cell)
        {
            if (((index < 0) || (index > (this.CellCount - 1))) || (cell == null))
            {
                return false;
            }
            this.Cells.Insert(index, cell);
            return true;
        }

        public bool IsFirstCell(ZYTextTableCell cell)
        {
            if (cell == null)
            {
                return false;
            }
            return (this.Cells.IndexOf(cell) == 0);
        }

        public bool IsLastCell(ZYTextTableCell cell)
        {
            if (cell == null)
            {
                return false;
            }
            return (this.Cells.IndexOf(cell) == (this.Cells.Count - 1));
        }

        public override bool isNewLine()
        {
            return false;
        }

        private void ModifyHeight(int offset)
        {
            this.Height += offset;
            foreach (ZYTextTableCell cell in this.Cells)
            {
                cell.Height += offset;
            }
        }

        public override bool RefreshSize()
        {
            ////int widthFix = 0;
            if (base.Attributes.Contains("width"))
            {
                base.intWidth = base.Attributes.GetInt32("width");
                if ((this.ownerTable != null) && (base.intWidth > this.ownerTable.Width))
                {
                    //widthFix = base.intWidth - this.ownerTable.Width;
                    base.intWidth = this.ownerTable.Width;
                }
            }
            else if (this.ownerTable != null)
            {
                base.intWidth = this.ownerTable.Width;
            }
            int height = 0;
            foreach (ZYTextElement cell in this.Cells)
            {
                cell.RefreshSize();
                if (((height < cell.Height) && (cell is ZYTextTableCell)) && !((ZYTextTableCell)cell).VMStart)
                {
                    height = cell.Height;
                }
                //if (widthFix > 0)
                //{
                //    int cellWidthPix = widthFix / this.Cells.Count;
                //    cell.Width -=cellWidthPix;
                //}
            }
            base.intHeight = height;
            this.RegulateRowHeight();
            return true;
        }

        public void RefreshRowHeight()
        {
            int height = 0;
            int width = 0;
            foreach (ZYTextTableCell cell in this.Cells)
            {
                cell.RefreshCellHeight();
                int dss = cell.RealTop;
                if (((height < cell.OwnerLine.Height) && (cell is ZYTextTableCell)) && !((ZYTextTableCell)cell).VMStart)
                {
                    cell.RefreshCellHeight();
                    height = cell.OwnerLine.Height;
                }
                width += cell.Width;
            }
            this.Width = width;
            base.intHeight = height;
            this.RegulateRowHeight();
        }

        public override bool RefreshView()
        {
            foreach (ZYTextElement element in this.Cells)
            {
                element.RefreshView();
            }
            return true;
        }

        public override int Top
        {
            get
            {
                return base.Top;
            }
            set
            {
                base.Top = value;
            }
        }

        public void RegulateColWidth(int colIndex, int offset)
        {
            if ((colIndex < 0) || (colIndex > (this.CellCount - 1)))
            {
                colIndex = this.CellCount - 1;
            }
            ZYTextTableCell cell = (ZYTextTableCell) this.Cells[colIndex];
            int num = cell.Width + offset;
            if (num < cell.MinWidth)
            {
                cell.Width = cell.MinWidth;
            }
            else
            {
                cell.Width = num;
            }
        }

        public void RegulateRowHeight()
        {
            foreach (ZYTextTableCell cell in this.Cells)
            {
                if (!cell.VMContinue)
                {
                    this.Height = this.RealHeight;
                    cell.Height = this.Height;
                }
            }
        }

        public void RegulateRowWidth()
        {
            int num = base.intWidth - 11;
            int num2 = 0;
            foreach (ZYTextTableCell cell in this.Cells)
            {
                num2 += cell.Width;
            }
            if (num > num2)
            {
                int num3 = num - num2;
                float num4 = 0f;
                if (num3 >= 1)
                {
                    num4 = ((float) num3) / ((float) this.CellCount);
                }
                if ((num4 > 0f) && (num4 < 1f))
                {
                    num4 = 1f;
                }
                foreach (ZYTextTableCell cell2 in this.Cells)
                {
                    if (num3 > 0)
                    {
                        cell2.Width += (int) num4;
                        num3--;
                    }
                }
            }
            else if (num < num2)
            {
                int num5 = num2 - num;
                float num6 = 0f;
                if (num5 > 1)
                {
                    num6 = ((float) num5) / ((float) this.CellCount);
                }
                if ((num6 > 0f) && (num6 < 1f))
                {
                    num6 = 1f;
                }
                foreach (ZYTextTableCell cell3 in this.Cells)
                {
                    if (num5 > 0)
                    {
                        cell3.Width -= (int) num6;
                        num5--;
                    }
                }
            }
        }

        public void RegulateVMRowHeight()
        {
            ZYTextTableCell cell = null;
            ZYTextTableRow row = null;
            for (int i = 0; i < this.CellCount; i++)
            {
                cell = (ZYTextTableCell)this.Cells[i];
                if (cell.VMStart)
                {
                    int num2 = 0;
                    for (int j = 0; j < cell.HoldRowsCount; j++)
                    {
                        row = (ZYTextTableRow)this.OwnerTable.Rows[this.RowIndex + j];
                        num2 += row.Height;
                    }

                    cell.Height = num2;
                    ZYTextTableRow row3 = ((ZYTextTableRow)this.OwnerTable.Rows[(cell.OwnerRow.RowIndex + cell.HoldRowsCount) - 1]);
                    int incHeight = cell.Height - num2;
                    if (incHeight > 0)
                    {
 
                    }
                    row3.IncHeight(incHeight);
                //    if (cell.Height <= num2)
                //    {
                //        cell.Height = num2;
                //    }
                //    else
                //    {
                //        ZYTextTableRow row3 = ((ZYTextTableRow)this.OwnerTable.Rows[(cell.OwnerRow.RowIndex + cell.HoldRowsCount) - 1]);
                //        int incHeight = cell.Height - num2;
                //        row3.IncHeight(incHeight);
                //    }
                }
            }
        }

        public void RegulateVMRowHeight2()
        {
            ZYTextTableCell cell = null;
            ZYTextTableRow row = null;
            for (int i = 0; i < this.CellCount; i++)
            {
                cell = (ZYTextTableCell)this.Cells[i];
                if (cell.VMStart)
                {
                    int num2 = 0;
                    for (int j = 0; j < cell.HoldRowsCount; j++)
                    {
                        row = (ZYTextTableRow)this.OwnerTable.Rows[this.RowIndex + j];
                        num2 += row.Height;
                    }
                    cell.Height = num2;
                }
            }
        }

        public bool RemoveCell(ZYTextTableCell cell)
        {
            if (cell == null)
            {
                return false;
            }
            if (!this.Cells.Contains(cell))
            {
                return false;
            }
            this.Cells.Remove(cell);
            this.Cells.Remove(cell);
            return true;
        }

        public bool RemoveCell(int index)
        {
            if ((index < 0) || (index > this.Cells.Count))
            {
                return false;
            }
            object obj2 = this.Cells[index];
            this.Cells.Remove(obj2);
            this.Cells.Remove(obj2);
            return true;
        }

        public override bool ToXML(XmlElement myElement)
        {
            return base.ToXML(myElement);
        }

        public int CellCount
        {
            get
            {
                return this.Cells.Count;
            }
        }

        public int MinHeight
        {
            get 
            {
                return base.Attributes.GetInt32("min-height");
            }
            set 
            {
                base.Attributes.SetValue("min-height", value);
            }
        }

        public ArrayList Cells
        {
            get
            {
                return this.ChildElements;
            }
        }

        public ZYTextTableCell FirstCell
        {
            get
            {
                return (ZYTextTableCell) this.Cells[0];
            }
        }

        public bool HasVMCell
        {
            get
            {
                foreach (ZYTextTableCell cell in this.Cells)
                {
                    if (cell.VMStart || cell.VMContinue)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public override int Height
        {
            get
            {
                if (MinHeight > base.intHeight)
                {
                    return MinHeight;
                }
                return base.intHeight;
            }
            set
            {
                base.intHeight = value;
            }
        }

        public int MinWidth
        {
            get
            {
                return 11;
            }
        }

        public ZYTextTable OwnerTable
        {
            get
            {
                return this.ownerTable;
            }
            set
            {
                this.ownerTable = value;
                foreach (ZYTextTableCell cell in this.Cells)
                {
                    cell.OwnerTable = value;
                }
            }
        }

        public int RealHeight
        {
            get
            {
                int realHeight = 0;
                foreach (ZYTextTableCell cell in this.Cells)
                {
                    if (!cell.VMStart && (realHeight < cell.RealHeight))
                    {
                        realHeight = cell.RealHeight;
                    }
                }
                return realHeight;
            }
        }

        public int RowIndex
        {
            get
            {
                return this.ownerTable.IndexOf(this);
            }
        }

        public override int Width
        {
            get
            {
                return base.intWidth;
            }
            set
            {
                if (value > this.MinWidth)
                {
                    base.intWidth = value;
                    base.Attributes.SetValue("width", value);
                }
            }
        }
    }
}

